package com.comp437.uno.view.playroomlist;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.comp437.uno.model.ClientService;
import com.comp437.uno.model.Model;
import com.comp437.uno.model.ClientService.LocalBinder;
import com.comp437.uno.view.login.Login;
import com.comp437.uno.view.login.SignUp;
import com.comp437.uno.view.playroom.PlayRoomView;
import com.example.comp437uno.R;

public class PlayRoomListView extends Activity{

	private ListView listView;
	private Model model;
	private Button bt_createNewRoom, bt_Logout;
	private TextView userid;
	Intent service;
	 public static Handler handler; 
	ClientService cs;
	boolean myBound=false;
	
	  public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.playroomlist);

	        model = Model.getModel();
	        model.setPRLV(this);
	        listView = (ListView) findViewById(R.id.listview_playroomlist_listView);
	        userid = (TextView) findViewById(R.id.textview_playroomlist_user_name);
	        bt_createNewRoom = (Button) findViewById(R.id.button_playroomlist_createroom);
	        bt_Logout = (Button) findViewById(R.id.button_playroomlist_logout);
	        userid.setText( getIntent().getExtras().get("username").toString());
	        bt_createNewRoom.setOnClickListener(new bt_createNewRoomListener());
	        bt_Logout.setOnClickListener(new bt_logoutListener());
	        model.askForRoomList();
	        service = new Intent(PlayRoomListView.this, ClientService.class);
	    	bindService(service, mConnection, Context.BIND_AUTO_CREATE);
	    	
	    	 handler = new Handler(){ 
	    		 
		            @Override 
		            public void handleMessage(Message msg) { 
		                super.handleMessage(msg); 
		                successfulCreateRoom((String)msg.obj);
		               
		             
		            } 
		             
		        }; 
	 }
	 
	  public void successfulCreateRoom(String roomid){
		  Intent intent=new Intent();
			intent.setClass(PlayRoomListView.this, PlayRoomView.class);
			intent.putExtra("roomID",  roomid);
			PlayRoomListView.this.startActivity(intent);
	  }
	
	  public void update(){
		  		
		        String[] ids = new String[model.Items.size()];
		        for (int i= 0; i < ids.length; i++){

		            ids[i] = Integer.toString(i+1);
		        }
		        //d
		        ItemAdapter adapter = new ItemAdapter(this,R.layout.playroomlist_item, ids);
		        listView.setAdapter(adapter);
		        model.setListViewHeightBasedOnChildren(listView);
	  }
	  
	  @Override
		public void onBackPressed()
		{
			   new AlertDialog.Builder(this)
		        .setTitle("Really Exit?")
		        .setMessage("Are you sure you want to Logout?")
		        .setNegativeButton(android.R.string.no, null)
		        .setPositiveButton(android.R.string.yes, new OnClickListener() {

		            public void onClick(DialogInterface arg0, int arg1) {
		            	model.logOut(PlayRoomListView.this);
		            	PlayRoomListView.super.onBackPressed();
		            }
		        }).create().show();
		}
		 class bt_logoutListener implements android.view.View.OnClickListener {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					model.logOut(PlayRoomListView.this);
					PlayRoomListView.super.onBackPressed();
					
				}
			}
		 class bt_createNewRoomListener implements android.view.View.OnClickListener {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					model.createRoom();
				
					
	           
				}
			}
			private ServiceConnection mConnection = new ServiceConnection() {

		        @Override
		        public void onServiceConnected(ComponentName className,
		                IBinder service) {
		            // We've bound to LocalService, cast the IBinder and get LocalService instance
		            LocalBinder binder = (LocalBinder) service;
		            cs = binder.getService();
		            model.setClientService(cs);
		            myBound = true;
		        }

		        @Override
		        public void onServiceDisconnected(ComponentName arg0) {
		            myBound = false;
		        }
		    };

	
}
